package cn.i96688.scanclient.barcode;

import cn.i96688.scanclient.bean.PayLog;
import cn.i96688.scanclient.utils.Message;

/**
 * 条码生产者
 * @author xys
 *
 */
public class BarcodeProducter {
//	private Logger log=Logger.getLogger(BarcodeProducter.class);
    private boolean quit;
    private Thread thread;
    private ScanBarcodeService scanBarcodeService;
    
    private static BarcodeProducter producter=new BarcodeProducter();
    public static BarcodeProducter getInstance(){
    	return producter;
    }
    private BarcodeProducter(){
        scanBarcodeService=new ScanBarcodeService();
    }
    public void startProduct() {        
        if(thread!=null && thread.isAlive()){
            return;
        }
        PayLog.getInstance().addLog(Message.getMsg("msg.product.lab1"));
        //停止命令监听
        thread=new Thread() {
            @Override
            public void run() {
            	PayLog.getInstance().addLog(Message.getMsg("msg.product.lab2"));
                while (!quit) {
                    try {
                        Thread.sleep(Long.MAX_VALUE);
                    } catch (Exception e) {
                        quit=true;
                    }
                }

                scanBarcodeService.stopScanBarcodeService();
                PayLog.getInstance().addLog(Message.getMsg("msg.product.lab3"));
                System.exit(0);
            }
        };
        thread.start();
        //生产启动
        new Thread() {
            @Override
            public void run() {
                scanBarcodeService.startScanBarcodeService();
            }
        }.start();
        
    }

	public void stopProduct() {
		if (thread != null) {
			thread.interrupt();
			PayLog.getInstance().addLog(Message.getMsg("msg.product.lab4"));
		}
	}
}
